<?php

  require_once "JooS/PHPackager/FS/Action/Interface.php";

  class JooS_PHPackager_FS_Action_Callback implements JooS_PHPackager_FS_Action_Interface {
    /**
     * @var mixed
     */
    private $_callback;

    /**
     * @var array
     */
    private $_args;

    /**
     * @return JooS_PHPackager_FS_Action_Callback
     */
    public static function newInstance() {
      return new self();
    }

    protected function  __construct() {
      $this->_callback = null;
      $this->_args = array();
    }
    
    /**
     * @param mixed $callback
     * @return JooS_PHPackager_FS_Action_Callback
     */
    public function setCallback($callback) {
      if (!is_callable($callback)) {
        require_once "JooS/PHPackager/FS/Action/Exception.php";

        throw new JooS_PHPackager_FS_Action_Exception("FS_Action's parameter \$callback must be callable");
      }
      $this->_callback = $callback;

      return $this;
    }

    /**
     * @return mixed
     */
    public function getCallback() {
      return $this->_callback;
    }

    /**
     * @param array $args
     * @return JooS_PHPackager_FS_Action_Callback
     */
    public function setArgs($args) {
      if (!is_array($args)) {
        require_once "JooS/PHPackager/FS/Action/Exception.php";

        throw new JooS_PHPackager_FS_Action_Exception("FS_Action's parameter \$args must be an array");
      }
      $this->_args = $args;

      return $this;
    }

    /**
     * @return array
     */
    public function getArgs() {
      return $this->_args;
    }

    public function  __invoke() {
      $callback = $this->getCallback();
      $args = $this->getArgs();

      if (is_null($callback)) {
        require_once "JooS/PHPackager/FS/Action/Exception.php";

        throw new JooS_PHPackager_FS_Action_Exception("FS_Action's callback must be set");
      }

      return call_user_func_array($callback, $args);
    }
  }
